package com.example.ezexcel.handler;



import com.alibaba.excel.exception.ExcelAnalysisException;
import com.example.ezexcel.enumerator.ServiceCode;
import com.example.ezexcel.ex.ServiceException;
import com.example.ezexcel.response.JsonResult;
import freemarker.core.InvalidReferenceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){
        log.warn("开始处理全局异常信息为："+e.getMessage());
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleNumberFormatException(NumberFormatException e){
        log.warn("出现数格式异常");
        return JsonResult.fail(ServiceCode.OPERATION_FAILED,"出现数格式异常");
    }

    @ExceptionHandler
    public JsonResult handleBindException(BindException e){
        log.warn("出现绑定异常");
        log.warn("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",e);
        FieldError fieldError = e.getFieldError();
        String message = fieldError.getDefaultMessage();
        return JsonResult.fail(ServiceCode.OPERATION_FAILED,message);
    }

    @ExceptionHandler
    public JsonResult handleConstraintViolationException(ConstraintViolationException e) {
        log.debug("全局异常处理器开始处理ConstraintViolationException");
        String message = null;
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            message = constraintViolation.getMessage();
        }
        return JsonResult.fail(ServiceCode.OPERATION_FAILED, message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult handleMethodArgumentNotValid(MethodArgumentNotValidException e) {
        log.info("全局异常处理器开始处理MethodArgumentNotValidException");

        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> errors = bindingResult.getAllErrors();
        StringBuilder errorMessage = new StringBuilder();
        for (ObjectError error : errors) {
            errorMessage.append(error.getDefaultMessage()).append("; ");
        }
        System.out.println(errorMessage.toString());
        return JsonResult.fail(ServiceCode.OPERATION_FAILED, errorMessage.toString());
    }


    @ExceptionHandler(ExcelAnalysisException.class)
    public JsonResult handleExcelAnalysisException(ExcelAnalysisException e) {
        log.error("出现 EasyExcel 解析异常: " + e.getMessage(), e);
        return JsonResult.fail(ServiceCode.OPERATION_FAILED, "Excel文件解析失败!请检查路径！");
    }

    @ExceptionHandler(InvalidReferenceException.class)
    public JsonResult handleInvalidReferenceException(InvalidReferenceException e) {
        log.error(" FreeMarker 模板出现了不存在数据" + e.getMessage(), e);
        return JsonResult.fail(ServiceCode.OPERATION_FAILED, "FreeMarker 模板出现了不存在数据");
    }


    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e){
        log.warn("出现Throwable错误");
        log.warn("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",e);
        String mes = "请检查参数是否齐全！";
        return JsonResult.fail(ServiceCode.OPERATION_FAILED,mes);

    }
}
